<template>
    <el-table :cell-class-name="rowClassName" :data="formatData"  :row-style="showRow" v-bind="$attrs">
      <!-- <el-table-column type="expand">
       <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item  v-for="(item,key) in props.row" :key="key" :label="key">
            <span>{{ props.row[key] }}</span>
          </el-form-item>
        </el-form>
      </template>
      </el-table-column> -->
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column label="操作" width="160" class-name="operation_cell">
        <template slot-scope="scope">
          <wl-tip-button icon="btn_xz1"  tips="新增" :isAdd="true" @click="rowAdd(scope.$index, scope.row)"></wl-tip-button>
          <wl-tip-button icon="btn_xq"  tips="查看" @click="rowEdit(scope.$index, scope.row)"></wl-tip-button>
          <wl-tip-button icon="btn_sc"  tips="删除" :isDel="true" @click="rowDelete(scope.$index, scope.row)"></wl-tip-button>
        </template>
      </el-table-column>
      <el-table-column class="column-dept"  label="部门名称" v-if="columns.length===0" width="250">
            <template  slot-scope="scope">
                <span v-for="space in scope.row._level" class="ms-tree-space" :key="space"></span>
                <span class="tree-ctrl" v-if="iconShow(0,scope.row)" @click="toggleExpanded(scope.$index)">
                    <svg-icon v-if="!scope.row._expanded" :class="[{select:scope.row._expanded,noselect:!scope.row._expanded}]" icon-class="btn_zk2" class-name="btn_zk2" />
                    <svg-icon v-else :class="[{select:scope.row._expanded,noselect:!scope.row._expanded}]" icon-class="btn_zk2" class-name="btn_zk2" />
                </span>
                {{scope.row.name}}
            </template>
      </el-table-column>
      <el-table-column  v-else v-for="(column, index) in columns" :key="column.value" :label="column.text" :width="column.width">
          <template slot-scope="scope_tree">
              <span v-if="index === 0" v-for="space in scope.row._level" class="ms-tree-space" :key="space"></span>
              <span class="tree-ctrl" v-if="iconShow(index,scope.row)" @click="toggleExpanded(scope.$index)">
                  <svg-icon v-if="!scope.row._expanded" :class="[{select:scope.row._expanded,noselect:!scope.row._expanded}]" icon-class="btn_zk2" class-name="btn_zk2" />
                  <svg-icon v-else :class="[{select:scope.row._expanded,noselect:!scope.row._expanded}]" icon-class="btn_zk2" class-name="btn_zk2" />
              </span>
              {{scope.row[column.value]}}
          </template>
      </el-table-column>
      <slot></slot>
    </el-table>
</template>


<script>
/**
  Auth: Lei.j1ang
  Created: 2018/1/19-13:59
*/
import treeToArray from "src/utils/eval";
import { OrganizationConst } from "src/consts/organization_const";
export default {
  name: "treeTable",
  props: {
    data: {
      type: [Array, Object],
      required: true
    },
    columns: {
      type: Array,
      default: () => []
    },
    evalFunc: Function,
    evalArgs: Array,
    expandAll: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    // 格式化数据源
    formatData: function() {
      let tmp;
      if (!Array.isArray(this.data)) {
        tmp = [this.data];
      } else {
        tmp = this.data;
      }
      const func = this.evalFunc || treeToArray;
      const args = this.evalArgs
        ? Array.concat([tmp, this.expandAll], this.evalArgs)
        : [tmp, this.expandAll];

      return func.apply(null, args);
    }
  },
  methods: {
    rowClassName(data) {
      if (data.column.label == "部门名称") {
        return "column-dept";
      }
      return "";
    },
    showRow: function(row) {
      var show;
      if (WLStore().state.org.isShow) {
        row.row.parent = null;
      }
      show = row.row.parent
        ? row.row.parent._expanded && row.row.parent._show
        : true;
      row.row._show = show;
      return show
        ? "animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;"
        : "display:none;";
    },
    // 切换下级是否展开
    toggleExpanded: function(trIndex) {
      WLCommit(OrganizationConst.CommitQuery, false);
      const record = this.formatData[trIndex];
      record._expanded = !record._expanded;
      if (record._expanded) {
        this.$emit("expand", record);
      }
    },
    // 图标显示
    iconShow(index, record) {
      return index === 0 && isNoEmpty(record.child_list);
      //   return index === 0 && record.hasChildren;
    },
    rowAdd(index, row) {
      this.$emit("add", row);
    },
    rowEdit(index, row) {
      this.$emit("edit", row);
    },
    rowDelete(index, row) {
      this.$emit("delete", row);
    }
  }
};
</script>

<style scoped>

.ms-tree-space {
  position: relative;
  top: 1px;
  display: inline-block;
  font-style: normal;
  font-weight: 400;
  line-height: 1;
  width: 18px;
  height: 14px;
}
.ms-tree-space::before {
  content: "";
}
.processContainer {
  width: 100%;
  height: 100%;
}
table td {
  line-height: 26px;
}

.tree-ctrl {
  position: relative;
  cursor: pointer;
  color: #2196f3;
  margin-left: -18px;
}

@keyframes myselect {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(180deg);
  }
}

@keyframes mynoselect {
  from {
    transform: rotate(180deg);
  }
  to {
    transform: rotate(0deg);
  }
}

.select {
  animation: myselect 0.5s infinite;
  animation-iteration-count: 1;
  transform: rotate(180deg);
}

.noselect {
  animation: mynoselect 0.5s infinite;
  animation-iteration-count: 1;
  transform: rotate(0deg);
}
</style>

<style rel="stylesheet/css">
@keyframes treeTableShow {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@-webkit-keyframes treeTableShow {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
</style>
